import { StType } from "@/constants/StType";
import { CreateItemFlowWindow } from "@/modules/model/modals/createItemFlow";
import { FlowFormData } from "@/modules/model/modals/createItemFlow/ItemFlowEditorModel";
import { modelService, shapeService } from "@/modules/model/service";
import { locale } from "@/util/locale";
import { PaneModel } from "./PaneModel";

export class ItemFlowPaneModel extends PaneModel {
  tableData: FlowFormData[] = [];
  sortByNameActive = false;

  async loadItemFlows() {
    this.tableData = [];
    const { projectId, modelId: connectorModelId } = this.tabsModel.data;
    // 通过连接器modelId, 活动边modelId, 消息modelId查询该模型上所有的项流
    const list = connectorModelId ? await modelService.getConnectorBindItemFlows(projectId!, connectorModelId, "true") : [];
    for (let it of list) {
      const name = it.name;
      const typeName = locale.getTByContent(it, it.modelTypeCnName, { en: "metaclass", zhCn: "modelTypeCnName" });
      const prefix = name ? name + ":" + typeName : typeName;
      // 名称
      const flow = {
        id: it.id,
        name: prefix + "[" + it.showAttribute.extraAttribute?.sourceName + " → " + it.showAttribute.extraAttribute?.targetName + "]",
        icon: it.icon
      };
      // 方项
      const flowDirection = {
        sourceId: it.showAttribute.sourceId || "",
        targetId: it.showAttribute.targetId || "",
        name: `从${it.showAttribute.sourceName}到${it.showAttribute.targetName}`
      };
      const itemInfo = await modelService.getItemFlowInfo(projectId!, it.id);

      // 项属性
      const itemProperty = {
        id: itemInfo.itemProperty?.instanceId || "",
        icon: itemInfo.itemProperty?.icon,
        name: itemInfo.itemProperty?.name
      };
      // 传递对象(多个传递项时就生成多条项流，以传递项为维度)
      if (itemInfo.conveyedItems && itemInfo.conveyedItems.length > 0) {
        for (let convey of itemInfo.conveyedItems) {
          this.tableData.push({
            flow,
            flowDirection,
            conveyedItems: [{ id: convey?.instanceId || "", name: convey.name, icon: convey.icon }],
            itemProperty: itemProperty || { id: "", name: "" }
          });
        }
      }
      // 未设置传递项的项流不显示在此列表中
      // else {
      //   this.tableData.push({
      //     flow,
      //     flowDirection,
      //     conveyedItems: [],
      //     itemProperty: itemProperty || { id: '', name: '' }
      //   });
      // }
    }
    this.loaded = true;
  }
  async handleAdd() {
    const { projectId, modelId } = this.tabsModel.data;
    const diagramModelId = await shapeService.getShapeDiagram(projectId as string, modelId as string);
    // 弹出新建项流的弹框
    await CreateItemFlowWindow.popup({
      projectId: projectId!,
      parentId: diagramModelId,
      connectorModelId: modelId,
      itemflowStId: StType["SysML::PortsAndFlows::ItemFlow"]
    });
  }

  /**
   * 移除事件，将该项流的某个传递项移除
   */
  async handleRemoveConvey(currentFlow: FlowFormData) {
    const { projectId } = this.tabsModel.data;
    const removeDto = {
      projectId: projectId!,
      instanceId: currentFlow.flow.id,
      relationId: this.tabsModel.data.modelId!,
      removeInstanceId: currentFlow.conveyedItems.length > 0 ? currentFlow.conveyedItems[0].id : ""
    };
    await modelService.removeConveyed(removeDto);
  }

  async loadDataWhenNotLoad() {
    if (!this.loaded) {
      await this.loadItemFlows();
    }
  }

  async fresh() {
    await this.loadItemFlows();
  }

  async clear() {
    this.tableData = [];
    this.loaded = false;
  }
}
